﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{
    public class AccountBLL : BaseBLL<Account, Guid>
    {
        public KeyValuePair<int, IEnumerable<Account>> Select(Guid customerId, QueryInfo qinfo)
        {
            var query = Rep.Entities;
                query = query.Where(p => p.Customer.Id==customerId);
           
            var count = query.Count();
            var list = query.OrderByDescending(p => p.Name).Paging(qinfo.Page, qinfo.PageSize).ToList();
            return new KeyValuePair<int, IEnumerable<Account>>(count, list);
        }

        /// <summary>
        /// 根据客户编号查询现金户头
        /// </summary>
        /// <param name="customerId">客户编号</param>
        /// <returns></returns>
        public Account GetCashAccountByCustomerId(Guid customerId)
        {
            var query = Rep.Entities;
            query = query.Where(p => p.Customer.Id == customerId && p.IsCash == true);
            return query.FirstOrDefault();
        }

        /// <summary>
        /// 根据客户编号查询户头列表
        /// </summary>
        /// <param name="customerId">客户编号</param>
        /// <returns></returns>
        public List<Account> GetListByCustomerId(Guid customerId)
        {
            var query = Rep.Entities;
            query = query.Where(p => p.Customer.Id == customerId);
            return query.ToList();
        }
    }
}
